package com.sanpu.kxe.jump.jsbridge;


import com.sanpu.kxe.base.BaseActivity;
import com.sanpu.kxe.business.login.bean.LoginOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.main.view.MainActivity;
import com.sanpu.kxe.business.member.provider.IMemberProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.jump.eventbus.RefreshChannelEvent;
import com.sanpu.kxe.jump.jsbridge.module.JBCallback;
import com.sanpu.kxe.jump.jsbridge.module.JSBridgeMethod;
import com.sanpu.kxe.jump.jsbridge.module.JsModule;
import com.sanpu.kxe.utils.AppContextUtil;
import com.sanpu.kxe.utils.IntentTool;
import com.sanpu.kxe.utils.PublicProviderUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Description：H5接口调用
 * Created by 薛勇军 on 2018/6/11.
 */

public class ServiceModule extends JsModule {

    @Override
    public String getModuleName() {
        return "service";
    }

    @JSBridgeMethod(methodName = "back")
    public void back(String msg, final JBCallback success, final JBCallback failure) {
        if (getContext() instanceof BaseActivity) {
            ((BaseActivity) getContext()).onBackPressed();
        }
    }

    @JSBridgeMethod(methodName = "finish")
    public void finish(String msg, final JBCallback success, final JBCallback failure) {
        if (getContext() instanceof BaseActivity) {
            ((BaseActivity) getContext()).finish();
        }
    }

    @JSBridgeMethod(methodName = "home")
    public void home(String msg, final JBCallback success, final JBCallback failure) {
        if (getContext() instanceof BaseActivity) {
            ((BaseActivity) getContext()).finish();
            AppContextUtil.finishAllExclByActivity(MainActivity.class.getName());
            IntentTool.startActivity(mContext, MainActivity.class);
        }
    }

    @JSBridgeMethod(methodName = "upgradeLvl")
    public void upgradeLvl(String msg, final JBCallback success, final JBCallback failure) {
        IMemberProvider provider = PublicProviderUtil.getProvider(IMemberProvider.class);
        if (provider != null) {
            provider.openMemberLevelsListActivity(getContext());
        }
    }

    @JSBridgeMethod(methodName = "openCreditEvent")
    public void openCreditEvent(String msg, final JBCallback success, final JBCallback failure) {
        IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (provider != null) {
            provider.openCreditEventActivity(getContext());
        }
    }

    @JSBridgeMethod(methodName = "openAdviceInitiate")
    public void openAdviceInitiate(String title, String type, final JBCallback success, final JBCallback failure) {
        IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (provider != null) {
            provider.openAdviceInitiateActivity(getContext(), title, type);
        }
    }

    @JSBridgeMethod(methodName = "backToHomePage")
    public void backToHomePage(String title, final JBCallback success, final JBCallback failure) {
        EventBus.getDefault().post(new RefreshChannelEvent());
        if (getContext() instanceof BaseActivity) {
            ((BaseActivity) getContext()).finish();
        }
    }

    @JSBridgeMethod(methodName = "getSessionId")
    public void getSessionId(String title, final JBCallback success, final JBCallback failure) {
        LoginOutput loginOutput = CacheManage.getInstance().getCache(CacheModel.LOGIN_KEY);
        String sessionId = "";
        if (loginOutput != null) {
            sessionId = loginOutput.getSessionId();
        }
        success.apply(sessionId);
    }
}
